subroutine srccsdg(ie, fij, fab, fia, & 
          iijkl, iijka, iabij, &
          iiabj, iabci,  &
          t1, t2, r1, r2, &
          fae, fmi, fme, &
          wmnij, wmbej, iabcd, icore, & 
          maxcore, escf, eccsd, & 
          r1_c, r2_c, t1_c, t2_c,iabcd_c, icore_c) 
use mod_iop
use mod_size
use mod_orbit
use mod_ioff
implicit none
include 'omp_lib.h'
!
integer prec2, isd2, igpu2, totgpu2
common/precsoc/prec2, isd2, igpu2, totgpu2
!
integer, intent(in) :: maxcore 
real*8, intent(inout) :: escf, eccsd
real*8, intent(inout) :: t1_c(*), t2_c(*),icore_c(*), iabcd_c(*)
real*8, intent(inout) :: r1_c(*), r2_c(*)
real*8, intent(inout),device :: ie(*), fij(*), fab(*), fia(*) 
real*8, intent(inout),device :: t1(*), t2(*), r1(*), r2(*) 
real*8, intent(inout),device :: iijka(*), iabij(*), iijkl(*)
real*8, intent(inout),device :: fae(*), fmi(*), fme(*)
real*8, intent(inout),device :: wmnij(*), wmbej(*)
real*8, intent(inout),device :: iiabj(*), iabci(*),  icore(*)
real*8, intent(inout),device :: iabcd(*)
!
integer nsize, i, j, a, b, icrsizg
integer maxvar, maxdim, iter
integer i0, i1, i2, i3, i4, i5, i6, i7, i8, i9
integer i10, i11, i12
real*8 dtmp, errdiis, eccsd2
real*8 eso, tt(20), rt(20)
!
real*8, external :: ddot, dnrm2
integer, external :: idsymsoc, irpdso
!
igpu=1; igpu2=1
ldebug = .false.
ldebug = .true.
!
call myicopyso(iabij, r2, nioi3)
call myicopyso(fia, r1, nifvo)
!
maxvar = nifvo + nioi3 
iter = 0 
call printdnrm2('iabij', iabij, 3)
!
!--iteration begins
!
do
iter = iter+1
write(6,*)
write(6,*)'=============in iteration:', iter
!
call cpu_time(tt(1)); call walltimeso(rt(1))
call denomt1(t1, r1, ie, 1)
call denomt2(t2, r2, ie, 1, 1, 1)
!
maxdim = 5
!
igpu = 0; igpu2 = 0
write(6,*)'begin dosodiis'
call myicopy_c(t1, t1_c, nifvo) 
call myicopy_c(t2, t2_c, nioi3) 
call dosodiis2(iter, t1_c, maxvar, maxdim, icore_c, &
              maxcore+maxvar, errdiis)
call myicopy_g(t1_c, t1, nifvo) 
call myicopy_g(t2_c, t2, nioi3) 
igpu = 1; igpu2 = 1
!
call effectt2(t2, t1,  icore, 1.d0, no, nv)
!
call calcene(t1, t2, fia, iabij, eccsd)
call calcene2(t1, t2, fia, iabij, eccsd2)
write(6,*)'CCSD energy', eccsd, eccsd2
write(6,*)'total energy', escf+eccsd, escf+eccsd2
!
call effectt2(t2, t1, icore, -1.d0, no, nv)
if(iter>29) exit
!
call cpu_time(tt(2)); call walltimeso(rt(2))
write(6,'(A, F10.3, F10.3)')'====time for energy     ', tt(2)-tt(1), rt(2)-rt(1)
!===========prapere for the begin 
call myicopyso(fab, fae, nifvv)
call myicopyso(fij, fmi, nifoo)
call myicopyso(fia, fme, nifvo)
call myicopyso(iijkl, wmnij, nioi1)
call myicopyso(iiabj, wmbej, nioi4)
call myicopyso(fia, r1, nifvo)
call myicopyso(iabij, r2, nioi3)
call cpu_time(tt(3)); call walltimeso(rt(3))
write(6,'(A, F10.3, F10.3)')'====time for prepare    ', tt(3)-tt(2), rt(3)-rt(2)
!
!==========calc for the large terms 
!
!------term needed in r1 an r2 calculations 
call cpu_time(tt(4)); call walltimeso(rt(4))
call wmbej_t1fj_iefbm(wmbej, iabci, &
                  t1, fae, icore, -1.d0, 1.d0) 
call cpu_time(tt(5)); call walltimeso(rt(5))
write(6,'(A, F10.3, F10.3)')'====time for wmbej large', tt(5)-tt(4), rt(5)-rt(4)
!==============================
!-----terms which are independent 
!=============================
!
!==========abci======================
call cpu_time(tt(6)); call walltimeso(rt(6))
   call r1ai_t2efmi_iefam(r1, t2, iabci, icore, -0.5d0)
   call r2abij_t1ei_iabej(r2, t1, iabci, icore, 1.d0)
   call calciabci(r1, r2, t1, t2, iabci, icore)
call cpu_time(tt(7)); call walltimeso(rt(7))
! call printdnrm2('r1', r1, 22)
! call printdnrm2('r2', r2, 3)
!
!==========abcd======================
if(igpuabcd==0) then
  write(6,*)'abcd would be calculated in cpu' 
  igpu=0; igpu2=0
  call zeroso(r1_c, nifvo)
  call zeroso(r2_c, nioi3)
  call calciabcd(r1_c, r2_c, t1_c, t2_c, iabcd_c, icore_c)
  igpu=1; igpu2=1
!  call calciabcd(r1, r2, t1, t2, iabcd_c, icore)
elseif(igpuabcd==1) then 
  call calciabcdgin(r1, r2, t1, t2, iabcd, icore)
endif
call cpu_time(tt(8)); call walltimeso(rt(8))
write(6,'(A, F10.3, F10.3)')'====time for iabcd      ', tt(8)-tt(7), rt(8)-rt(7)
!call printdnrm2('r1', r1, 22)
!call printdnrm2('r2', r2, 3)
!
!=======    wmbej   ========================================
!
call cpu_time(tt(9)); call walltimeso(rt(9))
    call calcwmbej(iijka, iabij, &
                t1, t2, r1, r2, &
                wmbej, icore)
call cpu_time(tt(10)); call walltimeso(rt(10))
write(6,'(A, F10.3, F10.3)')'====time for wmbej      ', tt(10)-tt(9), rt(10)-rt(9)
!
!==============left============================
 
call calcleft(iijka, iabij, &
          iiabj, t1, t2, r1, r2, &
          fae, fmi, fme, &
          wmnij, icore)
call cpu_time(tt(11)); call walltimeso(rt(11))
write(6,'(A, F10.3, F10.3)')'====time for left       ', tt(11)-tt(10), rt(11)-rt(10)
if(igpuabcd==0) then 
call myicopy_g(r1_c, icore, nifvo) 
call axpyso(nifvo, 1.d0, icore, 1, r1, 1)
call myicopy_g(r2_c, icore, nioi3) 
call axpyso(nioi3, 1.d0, icore, 1, r2, 1)
endif
!
!----
if(ldebug) then 
call printdnrm2('r1', r1, 22)
call printdnrm2('r2', r2, 3)
endif
!
write(6,'(A, F10.3, F10.3)')'====time for iteration  ', tt(11)-tt(1), rt(11)-rt(1)
!
enddo
write(6,*)'the final energy is', Eccsd2
open(555,file='CCSD_ENERGY')
write(555,*) ESCF, Eccsd2
close(555)
!
return
end 
